using System;
using FBroSharp.Event;
using FBroSharp.Lib;
using FBroSharp.Const;
using FBroSharp.Value;
using FBroSharp.Callback;

namespace FBroBrowserAutomation
{
    /// <summary>
    /// 同步 JavaScript 回调类
    /// 用于执行 JavaScript 并等待返回结果
    /// ⚠️ 这个类是 FBro 框架执行 JS 并获取返回值的标准做法！
    /// </summary>
    public class FBroSharpJsCallbackSyn : FBroSharpJsCallback
    {
        // 同步辅助类 - 用于线程同步等待
        public FBroSharpSynHelp help;

        public FBroSharpJsCallbackSyn()
        {
            // 初始化help类
            help = new FBroSharpSynHelp();
        }

        /// <summary>
        /// JavaScript 执行完成后的回调方法
        /// 根据返回类型保存数据到 help 中
        /// </summary>
        public override void Callback(FBroSharpJSRequestType type, IFBroSharpValue data, string error)
        {
            // 将返回的数据保存在help中
            if (help != null && help.IsValid)
            {
                switch (type)
                {
                    case FBroSharpJSRequestType.VALUE_ISVALID:
                        // 无效
                        help.SetStringData(error);
                        break;

                    case FBroSharpJSRequestType.VALUE_NULL:
                        // 空值
                        help.SetStringData("无值");
                        break;

                    case FBroSharpJSRequestType.VALUE_INT:
                        // 整数值
                        help.SetIntData(data.GetInt());
                        break;

                    case FBroSharpJSRequestType.VALUE_STRING:
                        // 文本值
                        help.SetStringData(data.GetString());
                        break;

                    case FBroSharpJSRequestType.VALUE_BOOL:
                        // 逻辑值
                        help.SetBoolData(data.GetBool());
                        break;

                    case FBroSharpJSRequestType.VALUE_DOUBLE:
                        // 双精度小数值
                        help.SetDoubleData(data.GetDouble());
                        break;

                    case FBroSharpJSRequestType.VALUE_DATA_STRING:
                        // 时间文本值
                        help.SetStringData(data.GetString());
                        break;

                    case FBroSharpJSRequestType.VALUE_ARRAY:
                        // 数组值
                        help.SetStringData(data.GetString());
                        break;

                    case FBroSharpJSRequestType.VALUE_ARRAY_BUFFER:
                        // 数组缓存值
                        help.SetStringData(data.GetString());
                        break;

                    case FBroSharpJSRequestType.VALUE_FUNCTION:
                        // 方法值
                        help.SetStringData(data.GetString());
                        break;

                    case FBroSharpJSRequestType.VALUE_OBJECT:
                        // 对象值（JSON）
                        help.SetStringData(data.GetString());
                        break;
                }

                // 停止等待，唤醒主线程
                help.StopEvent();
            }
        }
    }
}

